package com.example.androidshixi;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import java.util.ArrayList;
import java.util.List;

public class MallFragment extends Fragment {

    private static List<Product> productList = new ArrayList<>();
    private ListView productListView;
    private ImageButton addProductButton;
    private boolean isAdminMode = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mall, container, false);

        productListView = view.findViewById(R.id.product_list_view);
        addProductButton = view.findViewById(R.id.add_product_button);

        // 初始化商品列表
        initProductList();

        // 设置适配器
        ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(requireContext(), R.layout.product_item, R.id.product_name_text_view, productList) {
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                Product product = productList.get(position);
                ((ImageView) view.findViewById(R.id.product_image_view)).setImageResource(product.getImageResId());
                ((TextView) view.findViewById(R.id.product_name_text_view)).setText(product.getName());
                ((TextView) view.findViewById(R.id.product_price_text_view)).setText("￥" + product.getPrice());
                return view;
            }
        };
        productListView.setAdapter(adapter);

        // 处理商品列表项点击事件
        productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Product product = productList.get(position);
                Intent intent = new Intent(requireContext(), ProductDetailActivity.class);
                intent.putExtra("name", product.getName());
                intent.putExtra("description", product.getDescription());
                intent.putExtra("price", product.getPrice());
                startActivity(intent);
            }
        });

        // 根据管理员模式状态决定是否显示添加商品按钮
        if (isAdminMode) {
            addProductButton.setVisibility(View.VISIBLE);
        } else {
            addProductButton.setVisibility(View.GONE);
        }

        // 处理添加商品按钮点击事件
        addProductButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(requireContext(), AddProductActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    private void initProductList() {
        if (productList.isEmpty()) {
            // 初始化一些示例商品
            productList.add(new Product("商品1", "这是商品1的描述", 99.9));
            productList.add(new Product("商品2", "这是商品2的描述", 199.9));
        }
    }

    public static void addProduct(Product product) {
        productList.add(product);
    }

    // 设置管理员模式
    public void setAdminMode(boolean isAdminMode) {
        this.isAdminMode = isAdminMode;
        if (addProductButton != null) {
            if (isAdminMode) {
                addProductButton.setVisibility(View.VISIBLE);
            } else {
                addProductButton.setVisibility(View.GONE);
            }
        }
    }
}